package com.capgemini.remind.service;

import com.capgemini.remind.mapper.ReportMapper;
import com.capgemini.remind.pojo.Report;
import com.google.common.collect.LinkedListMultimap;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by LUZHANG on 7/7/2016.
 */
@Service
@Transactional
public class ReportService {
    public final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ReportMapper reportMapper;


	public List<Report> queryReport(Report report, String reportType) throws ParseException {
		List<Report> rps = new LinkedList<Report>();
		Date startDate = null, endDate = null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
		Calendar ca = Calendar.getInstance(Locale.CHINESE);
		int startMonth = 0, endMonth = 0, startYear = 0, endYear = 0;

		if (null != report.getStartDate() && !"".equals(report.getStartDate())) {
			startDate = sf.parse(report.getStartDate());
			startMonth = Integer.valueOf(report.getStartDate().split("-")[1]);
			startYear = Integer.valueOf(report.getStartDate().split("-")[0]);
		}
		if (null != report.getEndDate() && !"".equals(report.getEndDate())) {
			endDate = sf.parse(report.getEndDate());
			endMonth = Integer.valueOf(report.getEndDate().split("-")[1]);
			endYear = Integer.valueOf(report.getEndDate().split("-")[0]);
		}

		List<Date> months = new ArrayList<Date>();
		if (endDate.after(startDate) && (endYear - startYear <= 1)) {
			months.add(startDate);
			if (0 == endYear - startYear)
				for (int i = 0; i < endMonth - startMonth; i++) {
					ca.setTime(startDate);
					ca.add(Calendar.MONTH, i+1);
					months.add(ca.getTime());
				}
			else {
				for (int i = 0; i < 12 - startMonth; i++) {
					ca.setTime(startDate);
					ca.add(Calendar.MONTH, i+1);
					months.add(ca.getTime());
				}
				for (int i = endMonth - 1; i > 0; i--) {
					ca.setTime(endDate);
					ca.add(Calendar.MONTH, -i);
					months.add(ca.getTime());
				}
				months.add(endDate);
			}
		} else if(endDate.equals(startDate))
			months.add(startDate);
		else
			return rps;

		if("queryReturnRatePage".equals(reportType)) {
			int totalCount = 0, oneMonth = 0, twoMonth = 0, overTwo = 0, oneMonthAmount = 0,twoMonthAmount = 0,threeMonthAmount = 0;
			for (int i = 0; i < months.size(); i++) {
				rps.add(reportMapper.queryReturnRate(report.getDealerNum(), months.get(i), report.getMaintenanceItem(), totalCount, oneMonth,
						oneMonthAmount, twoMonth, twoMonthAmount, overTwo, threeMonthAmount));
				if(null == rps.get(i)) {
					rps.add(new Report());
				}
				rps.get(i).setSendDate(sf.format(months.get(i)));
				if(null == report.getMaintenanceItem() || "".equals(rps.get(i).getMaintenanceItem()))
					rps.get(i).setMaintenanceItem("全部");
				else
					rps.get(i).setMaintenanceItem(report.getMaintenanceItem());
			}
		} else if ("querySendHistoryAccount".equals(reportType)) {
			int tireTotalCount = 0, tireDup = 0, engineOilTotalCount = 0, engineOilDup = 0, brakeTotalCount = 0,
					brakeDup = 0;
			for (int i = 0; i < months.size(); i++) {
				rps.add(reportMapper.querySendHistoryAccount(report.getDealerNum(), months.get(i), tireTotalCount,
						tireDup, engineOilTotalCount, engineOilDup, brakeTotalCount, brakeDup,
						report.getMaintenanceItem()));
				if(null == rps.get(i)) {
					rps.add(new Report());
				}
				rps.get(i).setSendDate(sf.format(months.get(i)));
				// set below values to avoid looping for non-Empty maintenanceItems
				rps.get(i).setTotalCount(rps.get(i).getTireTotalCount());
				rps.get(i).setDuplicatedCount(rps.get(i).getTireDup());
				rps.get(i).setMaintenanceItem(report.getMaintenanceItem());
			}

			List<Report> rpts = new LinkedList<Report>();
//			LinkedListMultimap<String,Report> multimap = LinkedListMultimap.create();
			if (null == report.getMaintenanceItem() || "".equals(report.getMaintenanceItem())) {
				for (int i = 0; i < rps.size(); i++) {
					Report rp = rps.get(i);
					Report rpt = new Report();
//					rpt.setSendDate(rp.getSendDate());
//					rpt.setMaintenanceItem("轮胎");
//					rpt.setTotalCount(rp.getTireTotalCount());
//					rpt.setDuplicatedCount(rp.getTireDup());
//					
//					Report rpt1 = new Report();
//						rpt1.setSendDate(rp.getSendDate());
//					rpt1.setMaintenanceItem("机油");
//					rpt1.setTotalCount(rp.getEngineOilTotalCount());
//					rpt1.setDuplicatedCount(rp.getEngineOilDup());
//					
//					Report rpt2 = new Report();
//					rpt2.setSendDate(rp.getSendDate());
//					rpt2.setMaintenanceItem("制动");
//					rpt2.setTotalCount(rp.getBrakeTotalCount());
//					rpt2.setDuplicatedCount(rp.getBrakeDup());
//					multimap.put(rpt.getMaintenanceItem(), rpt);
//					multimap.put(rpt1.getMaintenanceItem(), rpt1);
//					multimap.put(rpt2.getMaintenanceItem(), rpt2);
					rpt.setSendDate(rp.getSendDate());
					rpt.setMaintenanceItem("全部");
					rpt.setTotalCount(rp.getTireTotalCount()); // total SMS sent
					// total count of customers whom was sent SMS
					rpt.setDuplicatedCount(rp.getTireDup());
					rpts.add(rpt);
				}
//				if(null != multimap.keySet()) {
//					Iterator<String> it = multimap.keySet().iterator();
//					while(it.hasNext())
//						rpts.addAll(multimap.get(it.next()));
//				}
				rps = rpts;
			}
		}

		return rps;
	}

}
